package oop.ex3.crosswords;

/**
 * An implementation of Crossword Entry Interface,<br>
 * this class represent a entry in the crossword
 * 
 * @author tsachiho
 * @author kobi_atiya
 * 
 */
public class MyCrosswordEntry implements CrosswordEntry {

    protected String            term;             // the term
    protected String            definition;       // the definition
    protected CrosswordPosition pos;              // the position in the puzzle
    private int                 overlappingAmount; // amount of overlapped letters in exists entries. used for
                                                   // iterator calculation only

    /**
     * Creating a new Entry
     * 
     * @param term - the term
     * @param def - the definition
     * @param pos - it's position in the crossword
     */
    public MyCrosswordEntry(String term, String def, CrosswordPosition pos) {
        this.term = term;
        this.definition = def;
        this.pos = new MyCrosswordPosition(pos.getX(), pos.getY(), pos.isVertical());
        overlappingAmount = 0;
    }

    /**
     * Copying Entry from entry
     * 
     * @param pos - the copied entry
     */
    public MyCrosswordEntry(CrosswordPosition pos) {
        this(null, null, pos);
    }

    @Override
    public CrosswordPosition getPosition() {
        return new MyCrosswordPosition(pos.getX(), pos.getY(), pos.isVertical());
    }

    @Override
    public String getDefinition() {
        return definition;
    }

    /**
     * return the overlapping amount
     * 
     * @return the overlapping amount
     */
    public int getOverlapping() {
        return overlappingAmount;
    }

    /**
     * Sets the overlapping amount
     * 
     * @param amount - the overlapping amount
     */
    public void setOverlapping(int amount) {
        overlappingAmount = amount;
    }

    @Override
    public String getTerm() {
        return term;
    }

    @Override
    public int getLength() {
        /* Returning length based on calculation in case of "scrumbble game", can be changed there */
        return (term != null) ? MyCrosswordGlossary.getlength(term) : 0;
    }

    @Override
    public String toString() {
        return pos + "-" + term + ":" + overlappingAmount;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof MyCrosswordEntry)) return false;

        MyCrosswordEntry entry = (MyCrosswordEntry) obj;

        // only if position, term and defintion are the same
        return (this.pos.equals(entry.getPosition()) && this.term.equals(entry.getTerm()) && this.definition
                .equals(entry.getDefinition()));
    }
}
